package com.chuhe.starry.controller;

import com.chuhe.common.domain.dto.TokenUserInfoDto;
import com.chuhe.common.domain.vo.resp.ApiResult;
import com.chuhe.common.domain.vo.resp.LoadGroupResp;
import com.chuhe.common.domain.vo.resp.LoadGroupSessionResp;
import com.chuhe.common.domain.vo.resp.SaveGroupReq;
import com.chuhe.common.utils.RedisComponet;
import com.chuhe.starry.service.GroupInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author 张楚河
 * @since 2025-05-12
 */
@RestController
@RequestMapping("/group")
@Api(value = "用户群组接口", tags = "用户群组相关API", description = "用户群组相关API")
public class GroupInfoController {
    @Autowired
    private GroupInfoService groupInfoService;
    @Autowired
    private RedisComponet redisComponet;

    @RequestMapping("/saveGroup")
    @ApiOperation("创建或修改群组")
    public ApiResult<?> saveGroup(@Valid SaveGroupReq req, HttpServletRequest request){
        TokenUserInfoDto tokenUserInfo = redisComponet.getTokenUserInfoByToken(request);
        return ApiResult.success(groupInfoService.saveGroup(req,tokenUserInfo));
    }

    @RequestMapping("/loadMyGroup")
    @ApiOperation("获取群组")
    public ApiResult<List<LoadGroupResp>> loadMyGroup(HttpServletRequest request){
        TokenUserInfoDto tokenUserInfo = redisComponet.getTokenUserInfoByToken(request);
        return ApiResult.success(groupInfoService.loadMyGroup(tokenUserInfo.getUserId()));
    }

    @RequestMapping("/getGroupInfo")
    @ApiOperation("获取群组详情")
    public ApiResult<LoadGroupResp> loadGroupDetail(HttpServletRequest request,@NotEmpty String groupId){
        TokenUserInfoDto tokenUserInfo = redisComponet.getTokenUserInfoByToken(request);
        return ApiResult.success(groupInfoService.getGroupDetail(tokenUserInfo.getUserId(),groupId));
    }

    @RequestMapping("/getGroupInfo4Chat")
    @ApiOperation("获取群组会话")
    public ApiResult<LoadGroupSessionResp> getGroupInfo4Chat(HttpServletRequest request, @NotEmpty String groupId){
        TokenUserInfoDto tokenUserInfo = redisComponet.getTokenUserInfoByToken(request);
        return ApiResult.success(groupInfoService.getGroupInfo4Chat(tokenUserInfo.getUserId(),groupId));
    }

    /**
     * todo 添加或者移除人员
     * @return
     */
    @RequestMapping(value = "/addOrRemoveGroupUser")
    public ApiResult<?> addOrRemoveGroupUser(HttpServletRequest request, @NotEmpty String groupId, @NotEmpty String selectContacts,
                                           @NotNull Integer opType) {
        TokenUserInfoDto tokenUserInfo = redisComponet.getTokenUserInfoByToken(request);
        groupInfoService.addOrRemoveGroupUser(tokenUserInfo, groupId, selectContacts, opType);
        return ApiResult.success(null);
    }

    /**
     * 解散群
     * @return
     */
    @RequestMapping(value = "/dissolutionGroup")
    public ApiResult<?> dissolutionGroup(HttpServletRequest request, @NotEmpty String groupId) {
        TokenUserInfoDto tokenUserInfo = redisComponet.getTokenUserInfoByToken(request);
        groupInfoService.dissolutionGroup(tokenUserInfo.getUserId(), groupId);
        return ApiResult.success(null);
    }


}

